package com.qujia.station.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.DateUtils;
import com.qujia.station.domain.OilEngine;
import com.qujia.station.domain.dto.OilEngineDto;
import com.qujia.station.domain.dto.OilEngineSumDto;
import com.qujia.station.mapper.OilEngineMapper;
import com.qujia.station.service.IOilEngineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OilEngineServiceImpl implements IOilEngineService {

    @Autowired
    private OilEngineMapper oilEngineMapper;


    @Override
    public List<OilEngine> selectOilEngine(OilEngineDto oilEngineDto) {

        OilEngine oilEngine = new OilEngine();
        if(Objects.nonNull(oilEngineDto)) {
            BeanUtils.copyProperties(oilEngineDto, oilEngine);
        }
        List<OilEngine> oilEngineList = oilEngineMapper.selectOilEngineList(oilEngine);
        if(CollectionUtil.isEmpty(oilEngineList)){
            log.info("selectOilEngine请求油机数据列表为空：[{}]", JSONObject.toJSONString(oilEngineDto));
            return null;
        }
        return oilEngineList;
    }

    @Override
    public OilEngineSumDto getOilEngineSum(OilEngineDto oilEngineDto) {
        OilEngine oilEngine = new OilEngine();
        if(Objects.nonNull(oilEngineDto)) {
            BeanUtils.copyProperties(oilEngineDto, oilEngine);
        }
        log.info("selectOilEngine 开始时间：[{}],结束时间：[{}]",oilEngine.getBeginTime(),oilEngine.getEndTime());
        return oilEngineMapper.getOilEngineSum(oilEngine);
    }

    @Override
    public List<String> getOilNameList(Long enterpriseId) {
        return oilEngineMapper.getOilNameList(enterpriseId);
    }

    @Override
    public List<String> getOilGunList(Long enterpriseId) {
        return oilEngineMapper.getOilGunList(enterpriseId);
    }

    @Override
    public List<OilEngine> selectOilEngineList(OilEngine oilEngine) {
        // 获取30分钟内的订单
        oilEngine.setBeginTime(DateUtils.beforeThirtyMinutes());
        return oilEngineMapper.selectOilEngineListByGun(oilEngine);
    }

    @Override
    public int updateOilEngine(OilEngine oilEngine) {
        if(Objects.isNull(oilEngine) || Objects.isNull(oilEngine.getId())){
            throw new BaseException("修改油机订单失败");
        }
        return oilEngineMapper.updateByPrimaryKeySelective(oilEngine);
    }
}
